
' TSlider
Type TSlider Extends TWidget
	' Menu
	Global stdPopupMenu:TPopupMenu
	
	Field value:Float
	Field standardValue:Float
	Field onSlide(widget:TWidget) 
	Field sliding:Int
	Field textSizeX:Int
	Field textSizeY:Int
	
	Field valMin:Float
	Field valMax:Float
	
	' Init
	Method Init(nID:String, nText:String, nX:Int, nY:Int, nWidth:Int, nHeight:Int)
		Self.InitWidget(nID, nText, nX, nY, nWidth, nHeight) 
		Self.valMin = 0.0
		Self.valMax = 1.0
		Self.value = Self.valMax
		Self.standardValue = Self.value
		Self.onSlide = Null
		
		Self.SetPopupMenu(TSlider.stdPopupMenu)
	End Method
	
	' SetText
	Method SetText(nText:String)
		Self.text = nText
		Self.textSizeX = TextWidth(nText)
		Self.textSizeY = TextHeight(nText)
	End Method
	
	' SetMinMax
	Method SetMinMax(nMin:Float, nMax:Float)
		Self.valMin = nMin
		Self.valMax = nMax
	End Method
	
	' GetMinValue
	Method GetMinValue:Float()
		Return Self.valMin
	End Method
	
	' GetMaxValue
	Method GetMaxValue:Float()
		Return Self.valMax
	End Method
	
	' SetStandardValue
	Method SetStandardValue(nVal:Float, apply:Int = True) 
		If apply
			Self.SetValue(nVal)
			Self.standardValue = Self.value
		Else
			Self.standardValue = (nVal - Self.valMin) / (Self.valMax - Self.valMin)
		EndIf
	End Method
	
	' SetValue
	Method SetValue(nVal:Float) 
		Self.SetValueRel((nVal - Self.valMin) / (Self.valMax - Self.valMin))
	End Method
	
	' SetValueRel
	Method SetValueRel(nVal:Float) 
		Self.value = nVal
		
		' OnSlide handler
		If Self.onSlide <> Null
			Self.onSlide(Self.GetAffectingWidget())
		EndIf
	End Method
	
	' GetValue
	Method GetValue:Float() 
		Return Self.valMin + (Self.value * (Self.valMax - Self.valMin))
	End Method
	
	' GetValueRel
	Method GetValueRel:Float()
		Return Self.value
	End Method
	
	' Update
	Method Update()
		Self.hovered = False
		Self.sliding = False
		
		If PointInRect(TInputSystem.GetMouseX(), TInputSystem.GetMouseY(), Self.screenX, Self.screenY, Self.rWidth, Self.rHeight)
			Self.hovered = True
			
			If TInputSystem.GetMouseHit(1)
				' OnClick handler
				If Self.onClick <> Null
					Self.onClick(Self.GetAffectingWidget())
				EndIf
			EndIf
			
			If TInputSystem.GetMouseHit(2) 
				' Popup
				If Self.popupMenu <> Null
					Self.popupMenu.SetAffectingWidget(Self)
					Self.popupMenu.Popup()
				EndIf
			EndIf
			
			If TInputSystem.GetMouseDown(1)
				Local nValue:Float = (TInputSystem.GetMouseX() - Self.screenX) / Float(Self.rWidth)
				
				If nValue <> Self.value
					Self.sliding = True
					Self.value = nValue
					
					' OnSlide handler
					If Self.onSlide <> Null
						Self.onSlide(Self.GetAffectingWidget())
					EndIf
				EndIf
			EndIf
		EndIf
	End Method
	
	' Draw
	Method Draw()
		.SetColor Self.r, Self.g, Self.b
		DrawRect Self.rX, Self.rY, Self.rWidth, Self.rHeight
		
		.SetAlpha Self.alpha
		.SetColor Self.textR, Self.textG, Self.textB
		DrawText Self.text, Self.rX + Self.rWidth / 2 - Self.textSizeX / 2, Self.rY + Self.rHeight / 2 - Self.textSizeY / 2
		
		DrawRectOutline Self.rX, Self.rY, Self.rWidth, Self.rHeight
		
		' Slider
		DrawRect Self.rX + Self.value * Self.rWidth - 2, Self.rY - 2, 4, Self.rHeight + 4
		
		If Self.hovered
			.SetAlpha 0.5
			.SetColor 255, 255, 255
			DrawRect Self.rX, Self.rY, Self.rWidth, Self.rHeight
		EndIf
		
		If Self.sliding
			.SetAlpha 0.5
			.SetColor 0, 0, 0
			DrawRect Self.rX, Self.rY, Self.rWidth, Self.rHeight
		EndIf
	End Method
	
	' Reset
	Method Reset() 
		Self.SetValueRel(Self.standardValue)
	End Method
	
	' SetToMinimum
	Method SetToMinimum() 
		Self.SetValueRel(0.0)
	End Method
	
	' SetToMaximum
	Method SetToMaximum() 
		Self.SetValueRel(1.0)
	End Method
	
	' ResetFunc
	Function ResetFunc(widget:TWidget) 
		TSlider(widget).Reset()
	End Function
	
	' SetToMinimumFunc
	Function SetToMinimumFunc(widget:TWidget) 
		TSlider(widget).SetToMinimum()
	End Function
	
	' SetToMaximumFunc
	Function SetToMaximumFunc(widget:TWidget)
		TSlider(widget).SetToMaximum()
	End Function
	
	' InitClass
	Function InitClass() 
		TSlider.stdPopupMenu = TPopupMenu.Create("_sliderPopUp")
		TSlider.stdPopupMenu.AddMenuEntry("Reset", TSlider.ResetFunc) 
		TSlider.stdPopupMenu.AddMenuEntry("Set to minimum", TSlider.SetToMinimumFunc) 
		TSlider.stdPopupMenu.AddMenuEntry("Set to maximum", TSlider.SetToMaximumFunc)
	End Function
	
	' Create
	Function Create:TSlider(nID:String, nText:String, nX:Int = 0, nY:Int = 0, nWidth:Int = 0, nHeight:Int = 0)
		Local widget:TSlider = New TSlider
		widget.Init(nID, nText, nX, nY, nWidth, nHeight)
		Return widget
	End Function
End Type
